package com.training1.core.common.global.signIn;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.common.utils.http.IPUtil;


/**
 * 登陆类
 * @author hl 2015-12-23
 *
 */
public class LoginInfoUtil {

	public static final String LOGININFO = "LOGININFO";
	
	private static ThreadLocal<LoginInfo> threadLocal = new ThreadLocal<LoginInfo>();
	
	private static final Logger LOG = LoggerFactory.getLogger(LoginInfoUtil.class);
	
	private static  void setLoginInfo(LoginInfo info){
		threadLocal.set(info);
	}
	
	/**
	 * 返回loginInfo
	 * @return
	 */
	public static LoginInfo getLoginInfo(){
		LoginInfo info = threadLocal.get();
		if(info==null){
			info = new SimpleLoginInfo();
		}
		return info;
	}
	
//	private static LoginInfo getLoginInfo(HttpServletRequest request){
//		LoginInfo info = (LoginInfo)request.getSession().getAttribute(LOGININFO);
//		if(info==null){
//			LOG.debug("用户未登陆");
//			return new SimpleLoginInfo();
//		}
//		return info;
//	}
	
	public static LoginInfo removeLoginInfo(){
		LoginInfo info = getLoginInfo();
		threadLocal.remove();
		return info;
	}
	
	private static LoginInfo createLoginInfo(UserEntity user,String ip){
		return new SimpleLoginInfo(user,ip);
	}
	
	/**
	 * 把登陆信息记录到session里
	 * @param request
	 * @param username
	 * @param ip 若不填则为request里取
	 */
	public static void setLoginInfoToSession(HttpServletRequest request,UserEntity user,String ip){
		if(StringUtils.isBlank(ip)){
			ip = IPUtil.getIp(request);
		}
		LoginInfo info = createLoginInfo(user, ip);
		LOG.info("用户登陆，用户名为{},ip为{},登陆时间为{}",info.getLoginUser().getUserAccount(),info.getLoginIp(),info.getLoginTime());
		HttpSession session = request.getSession();
		session.setAttribute(LOGININFO, info);
		setLoginInfo(info);
	}
	
	/**
	 * 把session里的登陆信息放到线程中
	 * @param request
	 */
	public static LoginInfo copyLoginToThread(HttpServletRequest request){
		Object info = request.getSession().getAttribute(LOGININFO);
		if(info!=null&& info instanceof LoginInfo){
			setLoginInfo((LoginInfo)info);
		}
		
		return (LoginInfo)info;
	}
	
	/**
	 * api端调用
	 * @param cellphone 手机号
	 */
	public static void setLoginCellphone(String cellphone){
		UserEntity user = new UserEntity();
		user.setUserAccount(cellphone);
		LoginInfo info = new SimpleLoginInfo(user,true);
		setLoginInfo(info);
	}
	/**
	 * 登出
	 * @param request
	 */
	public static void signOut(HttpServletRequest request){
		if(request!=null){
			HttpSession session = request.getSession();
			session.removeAttribute(LOGININFO);
			session.invalidate();
		}
		
		signOut(IPUtil.getIp(request));
	}
	
	private static void signOut(String ip){
		LoginInfo info = removeLoginInfo();
		LOG.info("用户主动登出，用户名为{},ip为{},登出时间为{}",info.getLoginName(),ip,new Date());
	}
	
	/**
	 * 判断是否登陆，并把登陆信息放到线程中
	 * @param request
	 * @return
	 */
	public static boolean isSignIn(){
		return getLoginInfo().isSignIn();		
	}
	
}
